<template>
  <div class="action-sheet" @click="hideActionSheet">
    <!-- 下面list部分要延时挂载 -->
    <ul class="action-sheet-list" :class="{ show: isShowList }">
      <li
        v-for="(item, index) in store.state.actionSheetConfig.list"
        :key="index"
        @click="clickHandler(index)"
      >
        {{ item }}
      </li>
      <li>取消</li>
    </ul>
  </div>
</template>

<script>
import { onMounted,ref } from 'vue';
import {useStore} from 'vuex'
export default {
    name:'ActionSheet',
    setup() {
        //实现点击任意处，都隐藏掉拉起的ActionSheet组件
        const store = useStore();
        function hideActionSheet(){
            store.commit('hideActionSheet');
        }
        //先判断FoodItem传过来的要干得事  是否事函数
        function clickHandler(index){
            //是函数的话就调仓库中的方法，这个方法通过索引区别了每个listitem要干得事情
            if(typeof store.state.actionSheetConfig.successCallBack==='function'){
                store.state.actionSheetConfig.successCallBack(index)
            }
        }
        
        //这里必须是从 仓库中取值，我们重新拿一个变量接受，不能直接修改仓库中isShowList的值
        let isShowList = ref(false);
        onMounted(()=>{
            setTimeout(()=>{
                isShowList.value = true
            },50)
        })

        return {store,hideActionSheet,clickHandler,isShowList}
    }
};
</script>

<style lang="scss" scoped>
.action-sheet {
  background-color: rgba(0, 0, 0, 0.8);
  width: 100%;
  height: 100%;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 9999;
  .action-sheet-list {
    background-color: gray;
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    transform: translateY(100%);
    transition: all 200ms linear;
    li {
      user-select: none;
      height: 35px;
      @include flexbox(row, center, center);
      background-color: #fff;
      border-bottom: 1px solid lightgray;
      &:last-child {
        margin-top: 5px;
      }
    }
    &.show {
      transform: translateY(0);
    }
  }
}
</style>